/*
 * This file is part of AudioStream.
 * Copyright (C) 2010 Justin Li and Jacob Wirth
 *
 *  AudioStream is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  AudioStream is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with AudioStream.  If not, see <http://www.gnu.org/licenses/>.
 *  
 */

package org.frustra.audiostream.mp3;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;

/**
 * Parses a File for ID3 data
 * Currently supports v2 and v3
 */

public class ID3Parser {
	/**
	 * Contains all ID3 headers
	 * Additional headers:
	 * ARTIST, TITLE
	 */
	private HashMap<String, String> id3Tags = new HashMap<String, String>();
	
	/**
	 * Constructor for ID3Parse
	 * @param f The mp3 file to load from
	 * @throws IOException
	 */
	public ID3Parser(File f) throws IOException {
		id3Tags.put("ARTIST", "Unknown");
		id3Tags.put("TITLE", "Unknown");
		
		byte[] buf = new byte[10];
		FileInputStream stream = new FileInputStream(f);
		stream.read(buf);
		if (new String(buf, 0, 3).equalsIgnoreCase("ID3") && buf[5] == 0)
		{
			if (buf[3] == 3 && buf[4] == 0)
			{
				int len = buf[9];
				len |= buf[8] << 8;
				len |= buf[7] << 16;
				len |= buf[6] << 24;
				buf = new byte[len];
				stream.read(buf);
				int i = 0;
				while (i < len)
				{
					try
					{
						if (buf[i] == 0 && buf[i + 1] == 0 && buf[i + 2] == 0 && buf[i + 3] == 0) break;
						String tag = new String(buf, i, 4);
						int len2 = (buf[i + 7] & 0x000000FF);
						len2 |= (buf[i + 6] & 0x000000FF) << 8;
						len2 |= (buf[i + 5] & 0x000000FF) << 16;
						len2 |= (buf[i + 4] & 0x000000FF) << 24;
						i += 10;
						String content = new String(buf, i + 1, len2 - 1);
						i += len2;
						if (tag.toUpperCase().startsWith("TPE"))
						{
							id3Tags.put("ARTIST", content);
						}
						else if (tag.toUpperCase().startsWith("TIT"))
						{
							id3Tags.put("TITLE", content);
						}
						id3Tags.put(tag, content);
					}
					catch (Exception e)
					{
						e.printStackTrace();
					}
				}
			}
			else if (buf[3] == 2 && buf[4] == 0)
			{
				int len = buf[9];
				len |= buf[8] << 8;
				len |= buf[7] << 16;
				len |= buf[6] << 24;
				buf = new byte[len];
				stream.read(buf);
				int i = 0;
				while (i < len)
				{
					try
					{
						if (buf[i] == 0 && buf[i + 1] == 0 && buf[i + 2] == 0) break;
						String tag = new String(buf, i, 3);
						int len2 = buf[i + 5];
						len2 |= buf[i + 4] << 8;
						len2 |= buf[i + 3] << 16;
						i += 6;
						if (i + len2 > buf.length || len2 < 0) break;
						String content = new String(buf, i + 1, len2 - 2);
						i += len2;
						if (tag.toUpperCase().startsWith("TP"))
						{
							id3Tags.put("ARTIST", content);
						} else if (tag.toUpperCase().startsWith("TT"))
						{
							id3Tags.put("TITLE", content);
						}
						id3Tags.put(tag, content);
					}
					catch (ArrayIndexOutOfBoundsException e)
					{
						break;
					}
					catch (Exception e)
					{
						e.printStackTrace();
					}
				}
			}
		}
	}
	
	public String get(String key) {
		return id3Tags.get(key);
	}
}
